﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;
using System.Data.Common;
using System.Data;
using System.Collections;
using System.IO;
using System.Xml.Serialization;

namespace AMStudio.Common
{
    public class ProductDAL
    {
        //private DataSet myDataset;

        public ProductDAL()
        {
            //myDataset = new DataSet();
            //Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("MachineConfig");
            //string sql = "select * from [products$]";
            //DbCommand cmd = db.GetSqlStringCommand(sql);

            //string productsTable = "[products$]";

            //// Retrieve the initial data.
            //db.LoadDataSet(cmd, myDataset, productsTable);

            //// Get the table that will be modified.
            //DataTable dTable = myDataset.Tables[productsTable];



        }
        public bool Add(ProductSettings item)
        {
            Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("UIDB");
            StringBuilder sb = new StringBuilder();
            sb.Append(
                "insert into products values(@Name,@FullName,@Name,@Comments,@Remark)");
            DbCommand cmd = db.GetSqlStringCommand(sb.ToString());
            db.AddInParameter(cmd, "@Name", DbType.String, item.Name);
            db.AddInParameter(cmd, "@Description", DbType.String, item.Description);
            db.AddInParameter(cmd, "@Comments", DbType.String, item.Comments);
            db.AddInParameter(cmd, "@Remark", DbType.String, item.Remark);
            return db.ExecuteNonQuery(cmd) > 0 ? true : false;
        }

        public bool Update(ProductSettings item)
        {
            Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("UIDB");
            StringBuilder sb = new StringBuilder();
            sb.Append("update products set ");
            //sb.Append("ControlName=@ControlName,");
            //sb.Append("[Name]=@Name,");
            sb.Append("[Description]=@Description,");
            sb.Append("[Comments]=@Comments,");
            sb.Append("[Remark]=@Remark,");
            //sb.Append("[Remark]=@Remark,");
            sb.Append(" where [Name]=@Name");
            DbCommand cmd = db.GetSqlStringCommand(sb.ToString());
            db.AddInParameter(cmd, "@Name", DbType.String, item.Name);
            db.AddInParameter(cmd, "@Description", DbType.String, item.Description);
            db.AddInParameter(cmd, "@Comments", DbType.String, item.Comments);
            db.AddInParameter(cmd, "@Remark", DbType.String, item.Remark);
            int r = db.ExecuteNonQuery(cmd);
            return r > 0 ? true : false; ;
        }

        public bool Delete(string name)
        {
            Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("UIDB");
            StringBuilder sb = new StringBuilder();
            sb.Append("delete from products ");
            sb.Append(" where Name=@Name");
            DbCommand cmd = db.GetSqlStringCommand(sb.ToString());
            db.AddInParameter(cmd, "@Name", DbType.String, name);
            return db.ExecuteNonQuery(cmd) > 0 ? true : false;
        }

        /// <summary>
        /// 查询所有信息
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, ProductSettings> SelectAllMapper()
        {
            var list = new Dictionary<string, ProductSettings>();
            Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("UIDB");
            DataAccessor<ProductSettings> myAccessor;
            myAccessor = db.CreateSqlStringAccessor("select * from [products]",
                MapBuilder<ProductSettings>.MapAllProperties().
                //Map(p => p.StationSetting).WithFunc(dataRecord =>
                //    {
                //        byte[] content = (byte[])dataRecord["Content"];
                //        MemoryStream ms = new MemoryStream();
                //        ms.Read(content, 0, content.Length);
                //        XmlSerializer xs = new XmlSerializer(typeof(Hashtable));
                //        Hashtable myConfig = xs.Deserialize(ms) as Hashtable;
                //        return new Hashtable();
                //    }).//将密码转换为"*",无法直接查看
                Build()
                );
            list = myAccessor.Execute()
                .ToDictionary<ProductSettings, string>
                ((item) =>
                    {
                        return item.Name;
                    });
            return list;
        }


    }
}
